package com.canbot.u05.activity.free

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.helper.DialogBuildHelper
import kotlinx.android.synthetic.main.free_time_hz_actiivty.*
import kotlinx.android.synthetic.main.free_time_hz_item.view.*

/**
 * Created by zmp on 2019/6/3 16:57
 * @author zmp
 * 空闲状态设置主界面
 */
class FreeTimeHzActivity : BaseActivityNoTitle() {


    private lateinit var arr: Array<Pair<String,Long>>
    private var resultTime: String? = null
    private lateinit var myAdapter: MyAdapter
    private var currentPosition: Int = 0
    private var hintTip: DialogBuildHelper.HintTipBuilder? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.free_time_hz_actiivty)
        val modeKey = intent.getIntExtra(TIME_MODE_KEY, 0)
        resultTime = intent.getStringExtra(RESULT_TIME)
        initView(modeKey)
    }

    private val arrInterval = arrayOf(
            Pair("5 min", 5 * 60L),
            Pair("10 min", 10 * 60L),
            Pair("15 min", 15 * 60L),
            Pair("30 min", 30 * 60L),
            Pair("一直闲逛", -1L)
    )

    private val arrSustain = arrayOf(
            Pair("5 min", 5 * 60L),
            Pair("10 min", 10 * 60L),
            Pair("15 min", 15 * 60L),
            Pair("30 min", 30 * 60L)
    )

    private val arrHZ = arrayOf(
            Pair("1 min/次", 5 * 60 * 1000L),
            Pair("5 min/次", 10 * 60 * 1000L),
            Pair("10 min/次", 15 * 60 * 1000L),
            Pair("15 min/次", 30 * 60 * 1000L)
    )

    private fun initView(modeKey: Int) {
        arr = when (modeKey) {
            TIME_MODE_HZ -> {
                arrHZ
            }
            TIME_MODE_INTERVAL -> {
                arrInterval
            }
            TIME_MODE_SUSTAIN -> {
                arrSustain
            }
            else -> {
                arrHZ
            }
        }

        for (index in arr.indices) {
            if (arr[index].first ==resultTime){
                currentPosition = index
            }
        }
        back_btn_head.setOnClickListener {
            finish()
        }

        saveTextView.setOnClickListener {
            if (currentPosition != -1) {
                setResult(Activity.RESULT_OK, Intent()
                        .putExtra(RESULT_TIME, arr[currentPosition].first)
                        .putExtra(RESULT_NUM, arr[currentPosition].second)
                )
                finish()
            } else {
                showHintDialog(getString(R.string.please_set_params));
            }
        }

        voiceRecycleView.layoutManager = LinearLayoutManager(this)
        myAdapter = MyAdapter(this, arr)
        voiceRecycleView.adapter = myAdapter
    }

    private fun showHintDialog(content: String) {
        if (hintTip == null) {
            hintTip = DialogBuildHelper.createHintTipDialog(this)
                    .setClickListener(null, true)
        }
        hintTip?.setContent(content, Gravity.CENTER)?.show()
    }

    internal inner class MyAdapter(context: Context, var arr: Array<Pair<String, Long>>) : RecyclerView.Adapter<MyHolder>() {

        private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            val inflate = layoutInflater.inflate(R.layout.free_time_hz_item, parent, false)
            return MyHolder(inflate)
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            val str = arr[position]
            holder.updateView(str.first, position)
        }

        override fun getItemCount(): Int {
            return arr.size
        }

    }

    internal inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        private var audioTextView: TextView = itemView.audioTextView
        private var audioCheckIv: ImageView = itemView.audioCheckIv

        init {
            itemView.setOnClickListener(this)
        }

        override fun onClick(v: View) {
            if (currentPosition != adapterPosition) {
                currentPosition = adapterPosition
                myAdapter.notifyDataSetChanged()
            }
        }

        fun updateView(str: String, position: Int) {
            audioTextView.text = str
            audioCheckIv.visibility = if (position == currentPosition) View.VISIBLE else View.GONE
        }
    }

    companion object {
        const val TIME_MODE_KEY = "timeModeKey"
        const val TIME_MODE_HZ = 0
        const val TIME_MODE_INTERVAL = 1
        const val TIME_MODE_SUSTAIN = 2
        const val RESULT_TIME = "resultTime"
        const val RESULT_NUM = "resultNum"

    }
}